package com.sjd.web.controller.farm;

import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.farm.domain.dto.goods.*;
import com.sjd.farm.service.SjdFarmGoodsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 四季豆农场商品controller
 * @date : 2022-01-28 11:06
 */
@RestController
@RequestMapping("/farm/goods")
@AllArgsConstructor
@Slf4j
public class SjdFarmGoodsController extends BaseController {

    @Autowired
    private SjdFarmGoodsService farmGoodsService;

    /**
     * 添加农场商品
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @PostMapping (value = "add")
//    @PreAuthorize("@ss.hasPermi('farm:goods:add')")
    public AjaxResult addFarmGoods(@RequestBody @Validated SjdWebSiteFarmGoodsAddRequestDTO requestDTO){
        return farmGoodsService.add(requestDTO, getLoginUser());
    }


    /**
     * 修改农场商品
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @PostMapping(value = "update")
//    @PreAuthorize("@ss.hasPermi('farm:goods:update')")
    public AjaxResult updateFarmGoods(@RequestBody @Valid SjdWebSiteFarmGoodsUpdateRequestDTO requestDTO) {
        return farmGoodsService.updateFarmGoods(requestDTO, getLoginUser());
    }

    /**
     * 农场商品上下线
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @PostMapping(value = "online")
//    @PreAuthorize("@ss.hasPermi('farm:goods:online')")
    public AjaxResult onlineOfflineGoods(@RequestBody @Valid SjdWebSiteFarmGoodsOnlineRequestDTO requestDTO) {
        return farmGoodsService.onlineOfflineGoods(requestDTO, getLoginUser());
    }


    /**
     * 删除农场商品
     *
     * @param farmGoodsId farmGoodsId
     * @return return
     */
    @PostMapping(value = "delete/{farmGoodsId}")
//    @PreAuthorize("@ss.hasPermi('farm:goods:delete')")
    public AjaxResult deleteFarmGoods(@PathVariable("farmGoodsId") Long farmGoodsId) {
        return farmGoodsService.deleteFarmGoods(farmGoodsId, getLoginUser());
    }

    /**
     * 查询农场商品
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @GetMapping()
//    @PreAuthorize("@ss.hasPermi('farm:goods:query')")
    public TableDataInfo queryFarmGoods(SjdWebSiteFarmGoodsQueryRequestDTO requestDTO) {
        startPage();
        List<SjdWebSiteFarmGoodsQueryResponseDTO> responseDTOList = farmGoodsService.webSiteQueryFarmGoods(requestDTO);
        return getDataTable(responseDTOList);
    }


}
